
using com.byt.webcore.utils;
using System.Security.Cryptography;
using System.Text;
using System;
using System.Collections;
namespace com.byt.webcore.security
{
public sealed class Encrypt {

    //private static Key KEY_INSTANCE = null;
    private static  string KEY_ALGORITHM = "DES";
    private static  string KEY_FILENAME = "WEB-INF/app.key";
    private static  string CIPHER_TRANSFORMATION = "DES/ECB/PKCS5Padding";

    private Encrypt() {
    }
    
    /*
     * Generar clave publica
     */
    /*
    public static Key getKey(ServletBase.Parameters p) {
        
        if (Encrypt.KEY_INSTANCE != null)
            return Encrypt.KEY_INSTANCE;

        Key key;
        File file = new File(AppConfig.getServerPath(p) + Encrypt.KEY_FILENAME);

        if (!file.exists()) {
            KeyGenerator generator = KeyGenerator.getInstance(Encrypt.KEY_ALGORITHM);
            generator.init(new SecureRandom());
            key = generator.generateKey();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(key);
            out.close();
        } else {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            key = (Key)in.readObject();
            in.close();
        }

        Encrypt.KEY_INSTANCE = key;
        return key;
        
    }
    */
    public static byte[] encrypt(ServletBase.Parameters p, string value)  {
        if(string.IsNullOrEmpty(value))
            return null;
        return encrypt(p, Encoding.UTF8.GetBytes(value));
    }
    

    public static byte[] encrypt(ServletBase.Parameters p, byte[] value)  {
        return value;
        /*
        if(value == null)
            return null;

        Key desKey = Encrypt.getKey(p);

        Cipher desCipher;
        desCipher = Cipher.getInstance(Encrypt.CIPHER_TRANSFORMATION);

        // Inicializar el Cipher para encriptar
        desCipher.init(Cipher.ENCRYPT_MODE, desKey);

        // encriptar
        byte[] ciphertext = desCipher.doFinal(value);
        
        return ciphertext;
         * */
    }

    public static byte[] decrypt(ServletBase.Parameters p, byte[] value)  {
        return value;

        /*
        if(value == null)
            return null;

        Key desKey = Encrypt.getKey(p);

        Cipher desCipher;
        desCipher = Cipher.getInstance(Encrypt.CIPHER_TRANSFORMATION);

        // Inicializar el cipher para desencriptar
        desCipher.init(Cipher.DECRYPT_MODE, desKey);

        // desencriptar
        byte[] ciphertext = desCipher.doFinal(value);

        return ciphertext;
         * */
    }


/*
    public static string generateSalt(int lon) {
        return "";
    }

    public static string SHA1(string message) {
        return Encrypt.SHA1(message, "");
    }

    public static string SHA1(string message, string salt) {
        if (message == null)
            message = "";
        message = salt + message;

        MessageDigest md;
        byte[] buffer, digest;
        string hash = "";

        try {
            buffer = message.getBytes("UTF-8");
            md = MessageDigest.getInstance("SHA1");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        md.update(buffer);
        digest = md.digest();

        for (byte b: digest)
            hash += string.format("%02x", b & 0xff);

        return hash;
    }
    */
}

}